import { createRouter, createWebHashHistory ,createWebHistory} from "vue-router";
import login from "@/views/login.vue";

const routes = [
  { path: "/", redirect: "/login" },
  {
    name: "login",
    path: "/login",
    component: login,
  },
  {
    name: "registered",
    path: "/registered",
    component: ()=>{return import("@/views/registered.vue")}
  },
  {
    name: "home",
    path: "/home/:userid?",
    component: () => import("@/views/home.vue"),
    children: [
      {
        name: "weather",
        path: "/weather/:userid?",
        component: () => import("@/views/weather/weather.vue"),
      },
      {
        name: "distance",
        path: "/distance/:userid?",
        component: () => import("@/views/tools/distance.vue")
      },
      {
        name: "map",
        path: "/map/:userid?",
        component: () => import("@/views/map.vue"),
      },
      {
        name: "coordinates",
        path: "/coordinates/:userid?",
        component: () => import("@/views/tools/coordinates.vue")
      },
      {
        name: "navigation",
        path: "/navigation/:userid?",
        component: () => import("@/views/navigation.vue")
      },
      {
        name: "transEvent",
        path: "/transEvent/:userid?",
        component: () => import("@/views/transEvent.vue")
      },
      {
        name: "statistical",
        path: "/statistical/:userid?",
        component: () => import("@/views/statistical.vue")
      },
      {
        name: "about",
        path: "/about/:userid?",
        component: () => import("@/views/about.vue"),
      },
      {
        name: "electricity",
        path: "/energy/electricity/:userid?",
        component: () => import("@/views/energy/electricity.vue")
      },
      {
        name: "forest",
        path: "/energy/forest/:userid?",
        component: () => import("@/views/energy/forest.vue")
      },
      {
        name: "incomeAndCost",
        path: "/residentsLive/incomeAndCost/:userid?",
        component: ()=>{return import("@/views/residentsLive/incomeAndCost.vue")},
      },
      {
        name: "gdp",
        path: "/gdp/:userid?",
        component: () => import("@/views/gdp.vue"),
      },
      {
        name: "epidemic_situation",
        path: "/epidemicSituation/:userid?",
        component: () => import("@/views/epidemic_situation.vue"),
      },
      {
        name: "dataDownload",
        path: "/dataDownload/:userid?",
        component: () => import("@/views/dataDownload.vue"),
      }
    ],
  },
 
];

const router = createRouter({
  history: createWebHashHistory(),
  routes,
});

router.beforeEach((to, from, next) => {
    if(to.fullPath == "/login")
    {
      next();
    }
    else if(to.fullPath == "/registered") {
      next()
    }
    else{
      if(window.sessionStorage.getItem("token") == null)
      {
        console.log("拦截了")
        next({name: "login"});
      }
      else{
        next()
      }
    }
});
export default router;
